/**
 * Project Name:jrx
 * File Name:PushUtils.java
 * Package Name:cn.bluemobi.queue.util.jpush
 * Date:2015年9月17日上午10:44:13
 * Copyright (c) 2015, bluemobi.cn All Rights Reserved.
 *
 */

package cn.bluemobi.app.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import cn.bluemobi.app.core.utils.PropertiesUtils;
import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * Description: 极光推送工具类 <br/>
 * Date: 2015年9月17日 上午10:44:13 <br/>
 * 
 * @author hut
 * @version
 * @see
 */
public class PushUtils {

    protected static final Logger log = LogManager.getLogger(PushUtils.class);

    private static final String appKey = PropertiesUtils.getProperty("JPUSH_APPKEY");

    private static final String masterSecret = PropertiesUtils.getProperty("JPUSH_MASTER_SECRET");

    public static final String TITLE = "Test from API example";

    public static final String ALERT = "康祺健身";

    public static final String MSG_CONTENT = "Test from API Example - msgContent";

    public static final String REGISTRATION_ID = "0900e8d85ef";

    public static final String TAG = "tag_api";

    public static void main(String[] args) {
        // testSendPushWithCustomConfig();
        Map<String, String> params = new HashMap<String, String>();
        sendPush("11", "测试极光", params);
    }

    public static void sendPush(String alias, String title, Map<String, String> params) {
        // HttpProxy proxy = new HttpProxy("localhost", 3128);
        // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_all_alias_alert(alias, title, params);

        try {
            PushResult result = jpushClient.sendPush(payload);
            log.info("Got result - " + result);

        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);

        } catch (APIRequestException e) {
            log.error("Error response from JPush server. Should review and fix it. ", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Code: " + e.getErrorCode());
            log.info("Error Message: " + e.getErrorMessage());
            log.info("Msg ID: " + e.getMsgId());
        }
    }

    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }

    public static PushPayload buildPushObject_all_alias_alert(String alias, String title, Map<String, String> params) {
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert(title)
                                .addPlatformNotification(
                                        IosNotification.newBuilder().setSound("default").setBadge(1).addExtras(params)
                                                .build())
                                .addPlatformNotification(AndroidNotification.newBuilder().addExtras(params).build())
                                .build()).setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
    }

    // public static PushPayload buildPushObject_all_alias_alert(String alias,
    // String title, Map<String, String> params) {
    // return PushPayload
    // .newBuilder()
    // .setPlatform(Platform.all())
    // .setAudience(Audience.alias(alias))
    // .setNotification(
    // Notification
    // .newBuilder()
    // .setAlert(title)
    // .addPlatformNotification(
    // IosNotification.newBuilder().setSound("default").setBadge(1).addExtras(params)
    // .build())
    // .addPlatformNotification(AndroidNotification.newBuilder().addExtras(params).build())
    // .build()).build();
    // }

    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag("tag1"))
                .setNotification(Notification.android(ALERT, TITLE, null)).build();
    }

    public static PushPayload buildPushObject_android_and_ios() {
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag("tag1"))
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert("alert content")
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().setTitle("Android Title").build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().incrBadge(1).addExtra("extra_key", "extra_value")
                                                .build()).build()).build();
    }

    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and("tag1", "tag_all"))
                .setNotification(
                        Notification
                                .newBuilder()
                                .addPlatformNotification(
                                        IosNotification.newBuilder().setAlert(ALERT).setBadge(5).setSound("happy")
                                                .addExtra("from", "JPush").build()).build())
                .setMessage(Message.content(MSG_CONTENT))
                .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
    }

    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(
                        Audience.newBuilder().addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
                                .addAudienceTarget(AudienceTarget.alias("alias1", "alias2")).build())
                .setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtra("from", "JPush").build()).build();
    }
}
